# -*- coding: utf-8 -*-
"""Common settings and globals."""


import os
from os.path import abspath, basename, dirname, join, normpath
from sys import path

from django.core.exceptions import ImproperlyConfigured


def get_env_variable(var_name):
  try:
    return os.environ[var_name]
  except KeyError:
    error_msg = "Set the %s env variable" % var_name
    raise ImproperlyConfigured(error_msg)

########## PATH CONFIGURATION
# Absolute filesystem path to the Django project directory:
DJANGO_ROOT = dirname(dirname(abspath(__file__)))

# Absolute filesystem path to the top-level project folder:
SITE_ROOT = dirname(DJANGO_ROOT)

# Site name:
SITE_NAME = basename(DJANGO_ROOT)

# Add our project to our pythonpath, this way we don't need to type our project
# name in our dotted import paths:
path.append(DJANGO_ROOT)
########## END PATH CONFIGURATION


########## DEBUG CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = False

# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
TEMPLATE_DEBUG = DEBUG
########## END DEBUG CONFIGURATION


########## MANAGER CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = (
    ('Your Name', 'your_email@example.com'),
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS
########## END MANAGER CONFIGURATION


########## DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}
########## END DATABASE CONFIGURATION


########## GENERAL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
TIME_ZONE = 'America/Los_Angeles'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = 'en-us'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True
########## END GENERAL CONFIGURATION


########## MEDIA CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = normpath(join(SITE_ROOT, 'media'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'
########## END MEDIA CONFIGURATION


########## STATIC FILE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = normpath(join(SITE_ROOT, 'assets'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (
    normpath(join(SITE_ROOT, 'static')),
)

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
########## END STATIC FILE CONFIGURATION


########## SECRET CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
# Note: This key should only be used for development and testing.
SECRET_KEY = r"257n)5_b$y2x%%!h+8fz61a%_3!#v^l&g%ndprxu@&=bf*y+6="
########## END SECRET CONFIGURATION


########## SITE CONFIGURATION
# Hosts/domain names that are valid for this site
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []
########## END SITE CONFIGURATION


########## FIXTURE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS
FIXTURE_DIRS = (
    normpath(join(SITE_ROOT, 'fixtures')),
)
########## END FIXTURE CONFIGURATION


########## TEMPLATE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
TEMPLATE_DIRS = (
    normpath(join(SITE_ROOT, 'templates')),
)
########## END TEMPLATE CONFIGURATION


########## MIDDLEWARE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#middleware-classes
MIDDLEWARE_CLASSES = (
    # Default Django middleware.
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
########## END MIDDLEWARE CONFIGURATION


########## URL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#root-urlconf
ROOT_URLCONF = '%s.urls' % SITE_NAME
########## END URL CONFIGURATION


########## APP CONFIGURATION
DJANGO_APPS = (
    # Default Django apps:
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Useful template tags:
    # 'django.contrib.humanize',

    # Admin panel and documentation:
    'django.contrib.admin',
    # 'django.contrib.admindocs',
)

THIRD_PARTY_APPS = (
    # Database migration helpers:
    'south',
    # Task scheduler:
    'djcelery',
    # Search framework:
    'haystack',
)

# Apps specific for this project go here.
LOCAL_APPS = (
    'basic_info',
    'news',
    'institutes',
    'companies',
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
########## END APP CONFIGURATION


########## LOGGING CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#logging
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}
########## END LOGGING CONFIGURATION


########## WSGI CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = '%s.wsgi.application' % SITE_NAME
########## END WSGI CONFIGURATION

########## PROJECT-WISE CONFIGURATION

def get_static_file_path(file_name):
    return normpath(join(SITE_ROOT, 'static', file_name))

DEFAULT_ENCODING = 'utf8'

CRAWL_STOCKS_INFO_FILE_PATH = get_env_variable('CRAWL_STOCKS_FILE_PATH')
CRAWL_STOCKS_INFO_FILES = {
    'eastmoney':get_static_file_path('eastmoney.ini'),
    'jrj': get_static_file_path('jrj.ini'),
    'sina': get_static_file_path('sina.ini'),
    '163': get_static_file_path('163.ini'),
    'csrc':get_static_file_path('csrc.ini'),
 }

CRAWL_STOCKS_INFO_FILE_NAME = {
    'eastmoney': 'eastmoney_',
    'sina': 'sina_',
    'jrj': 'jrj_',
    '163':'163_',
    'csrc':'csrc_industry_classify.txt',
    }

CRAWL_STOCKS_INFO_CATEGORY = {
    'industry': 'industry.txt',
    'location': 'location.txt',
    'concept': 'concept.txt',
    }

CRAWL_COLUMNS_DELIMITER = ' '
URL_OPEN_TIMEOUT = 60
DEFAULT_PAGE_NUMBER = 20
DEFAULT_STOCKS_TOTAL_NUMBER = 2000

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
        'URL': 'http://127.0.0.1:8983/solr',
     },
}

REDIS_DATA_ATTRIBUTE = {
    'title': 'title',
    'content': 'content',
    'pubtime': 'pubtime',
    'publisher': 'publisher',
    'stock_code': 'stockcode',
}

NEWS_SUMMARIZER_PARAMS = {
    'lex_rank_epsilon': 0.001,
    'lex_rank_damping_factor': 0.85,
    'df_threshold': 0.15,
    'max_keywords_returned': 10,
    'max_sentences_returned': 3,
    'sentence_cutoff': 5,
}

NEWS_SUMMARIZER_DATA_FILES = {
    'stop_words_file': get_static_file_path('stopwords.txt'),
    'financial_stopwords_file': get_static_file_path('financial_stopwords.txt'),
    'doc_freq_file': get_static_file_path('df.dat'),
    'keyword_freq_file': get_static_file_path('kf.dat'),
}

NEWS_SUMMARIZER_DELIMITERS = {
    'clause_delimiter': ',| |，|。|！|!|\n',
    'sentence_delimiter': '。|！|!|\n',
    'token_delimiter': '|',
}

# 新闻内容最长截断
NEWS_FILE_LENGTH_CUTOFF = 10000

# 新闻关键字分隔符
NEWS_KEYWORDS_DELIMITER = ' '

# 新闻中个股名称出现次数下限，超过这个下限表面此股票与该新闻相关
STOCK_NAME_OCCURRENCE_THRESHOLD = 2

# 券商名称后缀
INSTITUTE_NAME_SUFFIX = u'证券'

# FTP Connections
FTP_CONNECTION = {
    'url': 'dblftp.panguso.com',
    'user': 'caijingzhishitupu',
    'password': get_env_variable('FINANCIAL_DAILY_FTP_KEY'),
    'default_path': 'tougu',
    'encoding': 'gbk',
}

FTP_TOUGU_COLUMNS = {
    'institute': 0,
    'author': 1,
    'content': 2,
}

FTP_LINE_DELIMITER = '\n'
FTP_COLUMN_DELIMITER = ' '

# News Clusterer
NEWS_CLUSTERER_PARAMS = {      
    'distance_threshold': 0.7, 
    'cluster_num_limit': 30,
}

# News Deduper
NEWS_DEDUPER_PARAMS={
    'ham_dis_diff_threhold': 3,
    'hash_table_num': 4,
    'bits_per_byte': 8,
    'hash_bits_num': 32,
    'filter_val_threhold': 0.9,
}

# Moneyflow Processor
MONEYFLOW_SERVICE={
    'rsync_connection_str': 'search@10.10.128.30::CNINFO_INTERNAL/test/',
    'daily_file_dir': get_env_variable('MONEYFLOW_DAILY_FILE_DIR'),
    'file_name_prefix': 'jgz_flow_',
    'file_encoding': 'gbk',
}

MONEYFLOW_DATA_PATTERNS={
    'rt_header': '^#(sz|sh)list(\d{6}).xml',
    'rt_sec_code': '(\d{6})',
    'rt_sec_name': '(\*?\w+)',
    'rt_date_8': '(\d{8})',
    'rt_price': '(\-?\d+(.\d{2})?)',
    'rt_date_4': '(\d{4})',
    'rt_reserve': '(\d+)',
}

MONEYFLOW_DATA_PATTERN_GROUPS={
    'sec_code_group': 0,
    'sec_name_group': 0,
    'date_group': 0,
    'price_group': 0,
}

MONEYFLOW_DATA_COLUMN_IDS={
    'latest_price_id': 1,
    'total_flow_id': 5,
    'super_flow_id': 6,
    'big_flow_id': 7,
    'non_big_flow_id': 8,
}


MONEYFLOW_DATA_DELIMITER = '|'

API_DEFAULT_MAX_NUM_RESULTS = 10

# Question classifier
QUESTION_CLASSIFY_PARAMS = {
    # 重大事项
    'Reorg' : '(收|并)购|合并(整合)?|资产整合|重组|部门整合|裁员|减员',
    'Financing' : '融资|增发|注资|定向发行|募(资|集)',
    'Lawsuit' : '专利(权|纠纷)|诉讼|侵权|反倾销|贸易(壁垒|保护)|官司|起诉|(股权|产权)纠纷|开庭|庭审',
    'Projects' : '项目|进(展|度)|工期|延(期|误)|按时|完成|交付',
    'Shares' : '股(份|权)(转让|变动|变更)',
    'Options' : '股(份|权)激励|激励计划',
    # 财务数据
    'Dividend' : '分红',
    'Performance' : '业绩|成绩|经营状况|表现|考核',
    'KPI' : '财务(指标|数据)|(资产)*负债表|balance sheet|KPI',
    # 高管信息
    'ManagementChange' : '(高(层|管)|管理层)变动|(离|辞|撤|免)职',
    'ManagementWage' : '薪(资|酬)',
    'ManagementHolding' : '(增|减)持|持股',
}

#QA statistics max result counts
MAX_RESULTS_STOCK = 5
MAX_RESULTS_INDUSTRY = 3
MAX_RESULTS_CONCEPT = 3
MAX_RESULTS_LOCATION = 3

#QA question sub_categories
SUB_CATEGORY_PARAMS = {
    'MERGE_RESTRUCT' : u'并购重组',
    'REFINANCING' : u'再融资',
    'LITIGATION' : u'诉讼',
    'PROJECT_PROGRESS' : u'项目进展',
    'STOCK_CHANGE' : u'股份变动',
    'EQUITY_INCENTIVE' : u'股权激励',
    'ACHIEVEMENT' : u'业绩',
    'FINANCIAL_INDICATOR' : u'财务指标',
    'DIVIDEND' : u'分红',
    'MANAGER_CHANGE' : u'高管变动',
    'MANAGER_SALARY' : u'高管薪酬',
    'MANAGER_OWNERSHIP' : u'高管持股'
}

# Polarity configuration
POLARITY_DATA_FILES = {
    'feature_file_name': get_static_file_path('features_terms_tagged.txt'),
    'intermediate_transfer_table_file': get_static_file_path('status_transfer_table.txt'),
    'final_transfer_table_file': get_static_file_path('final_status_transfer_table.txt'),
}

POLARITY_FEATURE_TAGS = {
    'START': 'S',
    'TERMINAL': 'TER',
    'PREDICT': 'P',
    'POSITIVE': 'U',
    'NEUTRAL': 'N',
    'NEGATIVE': 'D',
    'UNCERTAIN': 'X',
    'FACT': 'T',
    'NEGATE_PRE': 'NGP',
    'NEGATE_NEX': 'NGN',
    'NEGATE': 'NG',
    'TRANSITION': 'Z',
    'AUX_POS': 'AU',
    'AUX_NEG': 'AD',
    'AUX_NEU': 'AN',
    'INCREASE': 'CU',
    'DECREASE': 'CD',
    'UNCHANGE': 'CN',
    'DEFAULT': 'DEF',
}

POLARITY_STATUSES = {
    'POSITIVE': 'POSITIVE',
    'NEGATIVE': 'NEGATIVE',
    'NEUTRAL': 'NEUTRAL',
    'UNCERTAIN': 'UNCERTAIN',
    'UNKNOWN': 'UNKNOWN',
}

POLARITY_ACTIONS = {
    'replace_L_R': 'rLR',
    'replace_LR_move': 'rLRm',
    'del_L_move': 'dLm',
    'del_R_move': 'dRm',
    'del_LR_move': 'dLRm',
    'replace_L_move': 'rLm',
    'del_L': 'dL',
    'del_LR': 'dLR',
    'move': 'm',
    'clear_move': 'cm',
    'clear_replace_quit': 'crq',
    'nop': 'nop',
}

# 传闻求证数据
XPATH_PREFIX = '//'

CHUANWEN_CONFIG = {
    'positive_base_url': 'http://www.p5w.net/stock/cwqz/y/list.xml',
    'negative_base_url': 'http://www.p5w.net/stock/cwqz/n/list.xml',
    # don't use the encoding embedded in the xml, could break the lxml parser
    'encoding': 'gbk',
    'input_date_format': '%Y-%m-%d %H:%M:%S',
    'output_date_format': '%Y-%m-%d',
}

CHUANWEN_DOC_ATTRIBUTE = {
    'Item': 'item',
    'SecName': 'title',
    'SecCode': 'stockcode',
    'Question': 'doccontent',
    'Answer': 'docabstract',
    'PubDate': 'pubDate',
    'Link': 'link',
}

########## END PROJECT-WISE CONFIGURATION
